package com.hjc.demo.clickhouse;

import okhttp3.Response;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowStreamReader;
import org.apache.arrow.vector.types.pojo.Field;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;

public class ClickhouseDemo {
    static String url = "http://1.15.51.162:8123";
    static String param = "select * from metrics.hits format RowBinaryWithNamesAndTypes";
    public static void main(String[] args) throws IOException {
        Response response = OkHttp3ClientUtil.getData(url,param);
//        InputStream stream = response.body().byteStream();
        byte[] bytes = response.body().bytes();
        System.out.println(bytes.length);

        System.out.println(new String(bytes));
        String string = new String(bytes);
        String sub = string.substring(46, 60);
        byte[] bytes1 = sub.getBytes(StandardCharsets.UTF_8);
        System.out.println(InetAddress.getByAddress(bytes1).getHostAddress());
        System.out.println(bytes1.length);
//        BufferAllocator bufferAllocator = new RootAllocator();
//        ArrowStreamReader reader = new ArrowStreamReader(stream, bufferAllocator);
//
//        reader.loadNextBatch();
//        VectorSchemaRoot root = reader.getVectorSchemaRoot();
//        System.out.println(root.getSchema());
//        for (Field field : root.getSchema().getFields()) {
//            System.out.println(field.getName());
//        }
////        System.out.println(root.getRowCount());
////        reader.loadNextBatch(); select
//        for (FieldVector fieldVector : root.getFieldVectors()) {
////            System.out.println(fieldVector.getClass());
////            System.out.println(fieldVector.getName());
////            System.out.println(fieldVector.getValueCount());
//            for (int i = 0; i < fieldVector.getValueCount(); i++) {
//                if (!fieldVector.isNull(i)) {
//                    System.out.println(String.valueOf(fieldVector.getObject(i)));
//                    System.out.println(fieldVector.getObject(i));
//                }
//            }
////            fieldVector.iterator().next().getObject(0);
//        }


    }
}


